package com.rolerealm.controller;

import com.rolerealm.common.result.Result;
import com.rolerealm.common.util.JwtUtil;
import com.rolerealm.common.util.ResultUtil;
import com.rolerealm.domain.vo.PageResult;
import com.rolerealm.domain.vo.UserFavoriteVO;
import com.rolerealm.service.UserFavoriteService;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/users/favorites")
@RequiredArgsConstructor
public class UserFavoriteController {

    private final JwtUtil jwtUtil;
    private final UserFavoriteService userFavoriteService;

    @PostMapping
    public Result<Void> addFavorite(@RequestHeader(name = "Authorization", required = false) String authorization,
                                    @RequestParam @NotNull Long characterId) {
        Long userId = jwtUtil.extractUserIdFromAuthorization(authorization);
        userFavoriteService.addFavorite(userId, characterId);
        return ResultUtil.success();
    }

    @DeleteMapping("/{characterId}")
    public Result<Void> removeFavorite(@RequestHeader(name = "Authorization", required = false) String authorization,
                                       @PathVariable Long characterId) {
        Long userId = jwtUtil.extractUserIdFromAuthorization(authorization);
        userFavoriteService.removeFavorite(userId, characterId);
        return ResultUtil.success();
    }

    @GetMapping
    public Result<PageResult<UserFavoriteVO>> pageFavorites(@RequestHeader(name = "Authorization", required = false) String authorization,
                                                            @RequestParam(defaultValue = "1") Integer page,
                                                            @RequestParam(defaultValue = "10") Integer size) {
        Long userId = jwtUtil.extractUserIdFromAuthorization(authorization);
        return ResultUtil.success(userFavoriteService.pageFavorites(userId, page, size));
    }
}


